package com.maxwell.googleplay.base;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadFactory;

import com.maxwell.googleplay.conf.Constants;
import com.maxwell.googleplay.factory.ThreadPoolFactory;
import com.maxwell.googleplay.holder.BaseHolder;
import com.maxwell.googleplay.holder.HomeHolder;
import com.maxwell.googleplay.holder.LoadMoreHolder;
import com.maxwell.googleplay.utils.UIUtils;

import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;

/**
 * @author yue
 * @创建时间 2016-3-30下午8:56:39
 * @描述 TODO
 * 
 */
public abstract class SuperBaseAdapter<ITEMBEANTYPE> extends BaseAdapter
		implements OnItemClickListener {

	public List<ITEMBEANTYPE> mDataSource = new ArrayList<ITEMBEANTYPE>();
	public static final int VIEWTYPE_LOADMORE = 0;
	public static final int VIEWTYPE_NORMAL = 1;
	private LoadMoreHolder mLoadMoreHolder;
	private LoadMoreTask mLoadMoreTask;
	private AbsListView mAbsListView;

	public SuperBaseAdapter(AbsListView absListView,
			List<ITEMBEANTYPE> dataSource) {
		super();
		absListView.setOnItemClickListener(this);
		mDataSource = dataSource;
		mAbsListView = absListView;
	}

	@Override
	public int getCount() {
		if (mDataSource != null) {
			return mDataSource.size() + 1;
		}
		return 0;
	}

	@Override
	public Object getItem(int position) {
		if (mDataSource != null) {
			return mDataSource.get(position);
		}
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	/***** ListView里面可以显示几种ViewType *****/
	@Override
	public int getViewTypeCount() {
		// 两种
		return 2;
	}

	@Override
	public int getItemViewType(int position) {
		// 如果划到底部(getCount() - 1)，切换到VIEWTYPE_LOADMORE
		if (position == getCount() - 1) {
			return VIEWTYPE_LOADMORE;
		} else {
			return getNormalType(position);
			
		}

	}

	/**
	 * @des 返回普通view的一个viewType
	 * @call 子类可以复写该方法，添加更多viewType
	 * @param position
	 * @return
	 */
	public int getNormalType(int position) {
		// TODO Auto-generated method stub
		return VIEWTYPE_NORMAL;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		BaseHolder<ITEMBEANTYPE> holder = null;
		// 初始化视图
		if (convertView == null) {
			if (getItemViewType(position) == VIEWTYPE_LOADMORE) {// 如果加载更多

				holder = (BaseHolder<ITEMBEANTYPE>) getLoadMoreHolder();
			} else {
				holder = getSpecialHolder(position);
			}

		} else {
			holder = (BaseHolder) convertView.getTag();
		}
		// 数据显示
		if (getItemViewType(position) == VIEWTYPE_LOADMORE) {
			// 如果加载更多
			if (hasLoadMore()) {
				performLoadMore();
			} else {
				mLoadMoreHolder
						.setDataAndRefreshHolderView(LoadMoreHolder.STATE_NONE);
			}

		} else {
			// 正常情况
			holder.setDataAndRefreshHolderView(mDataSource.get(position));
		}

		// 返回根布局
		return holder.mHolderView;
	}

	/**
	 * @desc 决定有没有加载更多，默认是返回true;子类可以复写此方法，如果子类返回的是false，就不会加载更多
	 * @call getView中滑到底部的时候去调用
	 * @return
	 */
	private boolean hasLoadMore() {

		return true;
	}

	/**
	 * @desc 滑到底之后，应该去拉去更多数据
	 * @call 滑到底部的时候执行
	 */
	private void performLoadMore() {
		if (mLoadMoreTask == null) {
			// 修改loadmore当前的视图为加载中
			int state = LoadMoreHolder.STATE_LOADING;
			mLoadMoreHolder.setDataAndRefreshHolderView(state);

			mLoadMoreTask = new LoadMoreTask();
			ThreadPoolFactory.getNormalPool().execute(mLoadMoreTask);
		}

	}

	class LoadMoreTask implements Runnable {

		@Override
		public void run() {
			// 真正开始请求网络加载更多数据
			List<ITEMBEANTYPE> loadMoreDatas = null;
			int state = LoadMoreHolder.STATE_LOADING;
			// 得到返回数据，处理结果
			try {
				loadMoreDatas = onLoadMore();

				if (loadMoreDatas == null) {// 没有更多数据
					state = LoadMoreHolder.STATE_NONE;
				} else {
					// 假如规定每页返回20条
					if (loadMoreDatas.size() < Constants.PAGESIZE) {
						state = LoadMoreHolder.STATE_NONE;
					} else {
						state = LoadMoreHolder.STATE_LOADING;
					}
				}

			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				state = LoadMoreHolder.STATE_RETRY;
			}

			// 定义中转的临时变量
			final int tempState = state;
			final List<ITEMBEANTYPE> tempLoadMoreDatas = loadMoreDatas;

			UIUtils.postTaskSafely(new Runnable() {

				@Override
				public void run() {
					// 刷新loadingmore视图
					mLoadMoreHolder.setDataAndRefreshHolderView(tempState);

					// 刷新ListView视图，返回加载更多之后的数据
					if (tempLoadMoreDatas != null) {
						mDataSource.addAll(tempLoadMoreDatas);
						notifyDataSetChanged();
					}
				}
			});
			mLoadMoreTask = null;
		}

	}

	/**
	 * @desc 真正加载更多数据
	 * @call 滑到底部的时候执行
	 */
	public List<ITEMBEANTYPE> onLoadMore() throws Exception {
		return null;
	};

	/**
	 * @return 加载更多的Holder
	 */
	private LoadMoreHolder getLoadMoreHolder() {
		if (mLoadMoreHolder == null) {
			mLoadMoreHolder = new LoadMoreHolder();
		}
		return mLoadMoreHolder;
	}

	/**
	 * @call getView方法中如果没有convertView的时候被创建
	 * @return 具体的BaseHolder的子类
	 */
	public abstract BaseHolder<ITEMBEANTYPE> getSpecialHolder(int position);

	/**
	 * @param parent
	 * @param view
	 * @param position
	 * @param id
	 */
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		//把Listview的头去掉
		if(mAbsListView instanceof ListView){
			position = position - ((ListView)mAbsListView).getHeaderViewsCount();
		}
		

		//
		if (getItemViewType(position) == VIEWTYPE_LOADMORE) {
			// 重新加载更多
			performLoadMore();
		} else {
			//
			onNormalItemClick(parent, view, position, id);
		}

	}

	/**
	 * @desc 点击普通条目对应的时间处理
	 * @param parent
	 * @param view
	 * @param position
	 * @param id
	 */
	public void onNormalItemClick(AdapterView<?> parent, View view,
			int position, long id) {
		
		
	}

}
